package com.enation.app.javashop.api.client;

import com.enation.app.javashop.framework.database.DaoSupport;
import com.enation.app.javashop.model.member.dos.Member;
import com.enation.app.javashop.model.member.dos.MemberCoupon;
import com.enation.app.javashop.model.member.dos.MemberPointHistory;
import com.enation.app.javashop.model.member.vo.BackendMemberVO;
import com.enation.app.javashop.model.security.ScanModuleDTO;
import com.enation.app.javashop.model.security.ScanResult;
import com.enation.app.javashop.service.member.MemberCouponManager;
import com.enation.app.javashop.service.member.MemberManager;
import com.enation.app.javashop.service.member.MemberPointManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 会员相关api
 *
 * @author zh
 * @version v7.0
 * @date 18/7/30 下午8:10
 * @since v7.0
 */
@Api(description = "会员相关api")
@RestController
@RequestMapping("/client/members")
@Validated
@ApiIgnore
public class MemberClientController {

    @Autowired
    private MemberManager memberManager;

    @Autowired
    private MemberPointManager memberPointManager;

    @Autowired
    private DaoSupport memberDaoSupport;

    @Autowired
    private MemberCouponManager memberCouponManager;

    @ApiOperation(value = "获取会员详细", response = Member.class)
    @GetMapping("/{member_id}")
    @ApiImplicitParam(name = "member_id", value = "会员id", required = true, dataType = "int", paramType = "path")
    public Member getMember(@ApiIgnore @PathVariable("member_id") Long memberId) {
        return memberManager.getModel(memberId);
    }

    @GetMapping("/login/zero")
    public void loginNumToZero() {
        memberManager.loginNumToZero();
    }

    @PutMapping("/login/add-num/{member_id}")
    public void updateLoginNum(@ApiIgnore @PathVariable("member_id") Long memberId, Long now) {
        memberManager.updateLoginNum(memberId, now);
    }

    @PostMapping("/{id}")
    public Member edit(@RequestBody Member member, @PathVariable("id") Long id) {

        return memberManager.edit(member, id);
    }

    @PostMapping("/point")
    public void pointOperation(@RequestBody MemberPointHistory memberPointHistory) {

        memberPointManager.pointOperation(memberPointHistory);
    }

    @GetMapping("/all-member-ids")
    public List<String> queryAllMemberIds() {

        return memberManager.queryAllMemberIds();
    }

    @GetMapping("/coupon/{member_id}/{mc_id}")
    public MemberCoupon getModel(@PathVariable("member_id") Long memberId, @PathVariable("mc_id") Long mcId) {
        return memberCouponManager.getModel(memberId, mcId);
    }

    @PostMapping("/coupon/{mc_id}/used")
    public void usedCoupon(@PathVariable("mc_id") Long mcId, @RequestParam("order_sn") String orderSn) {
        memberCouponManager.usedCoupon(mcId,orderSn);
    }

    @PostMapping("/coupon/{member_id}/{coupon_id}")
    public void receiveBonus(@PathVariable("member_id") Long memberId, @RequestParam("member_name") String memberName,@PathVariable("coupon_id") Long couponId) {

        memberCouponManager.receiveBonus(memberId, memberName,couponId);

    }

    @GetMapping("/new")
    public List<BackendMemberVO> newMember(@RequestParam("length") Integer length) {
        return memberManager.newMember(length);
    }

    @RequestMapping(value = "/scan-module", method = RequestMethod.POST)
    public ScanResult scanModule(@RequestBody ScanModuleDTO scanModuleDTO) {
        return this.memberManager.scanModule(scanModuleDTO);
    }
    @GetMapping(value = "/resign")
    public void reSign(){
        memberManager.reSign();
    }

    @GetMapping(value = "/repair/{member_id}")
    public void repair(@PathVariable("member_id")Long memberId){
        memberManager.repair(memberId);
    }
    @GetMapping(value = "/sleep")
    public void sleepMember(@RequestParam("time_stamp")long timeStamp){
        memberManager.sleepMember(timeStamp);
    }
}
